﻿using Microsoft.AspNetCore.Mvc;
using System.Data;
using System.Web;
using Think9.Models;
using Think9.Services.Base;
using Think9.Services.Table;

namespace Think9.Controllers.Basic
{
    [Area("SysTable")]
    public class TbEventController : BaseController
    {
        private TbEventService tbEvent = new TbEventService();
        private TbBasicService tbServer = new TbBasicService();
        private TbFieldService tbFieldService = new TbFieldService();
        private ComService comService = new ComService();

        [HttpGet]
        public ActionResult List(string tbid)
        {
            TbBasicService tbBasicService = new TbBasicService();
            TbBasicEntity model = tbBasicService.GetByWhereFirst("where TbId='" + tbid + "'");
            if (model == null)
            {
                return Json("录入表对象为空！");
            }

            model.isAux = string.IsNullOrEmpty(model.isAux) ? "2" : model.isAux;
            if (model.isAux == "1")
            {
                return Json("不能对辅助表进行此操作！");
            }

            ViewBag.tbid = tbid;
            return View();
        }

        [HttpGet]
        public JsonResult GetList(PageInfoEntity pageInfo, string tbid)
        {
            string where = "where TbId='" + tbid + "' ";
            var result = new { code = 0, msg = "", count = 999999, data = tbEvent.GetByWhere(where) };
            return Json(result);
        }

        [HttpGet]
        public ActionResult Add(string tbid)
        {
            ViewBag.tbid = string.IsNullOrEmpty(tbid) ? "" : tbid;
            ViewBag.SelectList = tbEvent.GetTbEventCustomizeList(tbid);

            return View();
        }

        [HttpGet]
        public ActionResult Edit(string id)
        {
            var model = tbEvent.GetByWhereFirst("where Id=@Id", new { Id = id });
            if (model != null)
            {
                return View(model);
            }
            else
            {
                return Json("数据不存在！");
            }
        }

        [HttpPost]
        public ActionResult Edit(string id, TbEventCustomizeEntity model)
        {
            model.ExecuteSql = model.ExecuteSql.Replace("&#xA;", "").Replace("\n", " ");

            string where = "where Id=" + id + "";
            string updateFields = "ExecuteSql,OrderNo,FullName,Remarks";

            var result = tbEvent.UpdateByWhere(where, updateFields, model) > 0 ? SuccessTip("操作成功，属性修改需重新生成才能生效") : ErrorTip("编辑失败");
            return Json(result);
        }

        [HttpPost]
        public ActionResult Add(string tbid, TbEventCustomizeEntity model)
        {
			model.ExecuteSql = model.ExecuteSql.Replace("&#xA;", "").Replace("\n", " ");
			model.TbId = tbid;
            if (tbEvent.Insert(model))
            {
                return Json(SuccessTip("操作成功，属性修改需重新生成才能生效"));
            }
            else
            {
                return Json(ErrorTip("操作失败"));
            }
        }

        [HttpGet]
        public ActionResult Delete(string id)
        {
            string err = tbEvent.Delete(id);

            if (err == "")
            {
                return Json(SuccessTip("删除成功，属性修改需重新生成才能生效"));
            }
            else
            {
                return Json(ErrorTip(err));
            }
        }

        public ActionResult GetTableFieldsList(string tbid)
        {
            var result = new { code = 0, msg = "", count = 999999, data = tbFieldService.GetTbColumList(tbid) };

            return Json(result);
        }

        [HttpGet]
        public ActionResult TempletInsert(string tbid)
        {
            ViewBag.tbid = string.IsNullOrEmpty(tbid) ? "" : tbid;
            //可选择的数据源绑定
            DataTable dt = tbServer.GetMainAndGridTb();
            ViewBag.FromTbList = DataTableHelp.ToEnumerable<valueTextEntity>(dt);

            ViewBag.FieldList = tbFieldService.GetFieldistByIdStr(string.IsNullOrEmpty(tbid) ? "" : tbid);

            return View();
        }

        [HttpGet]
        public ActionResult TempletUpdate(string tbid)
        {
            ViewBag.tbid = string.IsNullOrEmpty(tbid) ? "" : tbid;
            //可选择的数据源绑定
            DataTable dt = tbServer.GetMainAndGridTb();
            ViewBag.FromTbList = DataTableHelp.ToEnumerable<valueTextEntity>(dt);

            ViewBag.FieldList = tbFieldService.GetFieldistByIdStr(string.IsNullOrEmpty(tbid) ? "" : tbid);

            return View();
        }

        [HttpGet]
        public ActionResult TempletDelete(string tbid)
        {
            ViewBag.tbid = string.IsNullOrEmpty(tbid) ? "" : tbid;
            //可选择的数据源绑定
            DataTable dt = tbServer.GetMainAndGridTb();
            ViewBag.FromTbList = DataTableHelp.ToEnumerable<valueTextEntity>(dt);

            ViewBag.FieldList = tbFieldService.GetFieldistByIdStr(string.IsNullOrEmpty(tbid) ? "" : tbid);

            return View();
        }
    }
}